﻿using System;
using System.Threading;

namespace NeroConnect.Network
{
	class ConnectStream : System.IO.Stream
	{
		System.IO.Stream _stream;

		public ConnectStream(System.IO.Stream stream)
		{
			_stream = stream;
		}

		public override bool CanRead { get { return _stream.CanRead; } }
		public override bool CanSeek { get { return _stream.CanSeek; } }
		public override bool CanTimeout { get { return _stream.CanTimeout; } }
		public override bool CanWrite { get { return _stream.CanWrite; } }
		public override long Length { get { return _stream.Length; } }
		public override long Position { get { return _stream.Position; } set { _stream.Position = value; } }
		public override int ReadTimeout { get { return _stream.ReadTimeout; } set { _stream.ReadTimeout = value; } }
		public override int WriteTimeout { get { return _stream.WriteTimeout; } set { _stream.WriteTimeout = value; } }

		public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
		{
			try
			{
				return _stream.BeginRead(buffer, offset, count, callback, state);
			}
			catch (System.Net.WebException ex)
			{
				throw new WebExceptionImpl(ex);
			}
            catch (System.IO.IOException ex)
            {
                throw new WebExceptionImpl(ex);
            }
		}

		public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
		{
			try
			{
				return BeginWrite(buffer, offset, count, callback, state);
			}
			catch (System.Net.WebException ex)
			{
				throw new WebExceptionImpl(ex);
			}
            catch (System.IO.IOException ex)
            {
                throw new WebExceptionImpl(ex);
            }
		}

		public override void Close()
		{
			try
			{
				_stream.Close();
			}
			catch (System.Net.WebException ex)
			{
				throw new WebExceptionImpl(ex);
			}
            catch (System.IO.IOException ex)
            {
                throw new WebExceptionImpl(ex);
            }
		}

		public override int EndRead(IAsyncResult asyncResult)
		{
			try
			{
				return EndRead(asyncResult);
			}
			catch (System.Net.WebException ex)
			{
				throw new WebExceptionImpl(ex);
			}
            catch (System.IO.IOException ex)
            {
                throw new WebExceptionImpl(ex);
            }
		}

		public override void EndWrite(IAsyncResult asyncResult)
		{
			try
			{
				_stream.EndWrite(asyncResult);
			}
			catch (System.Net.WebException ex)
			{
				throw new WebExceptionImpl(ex);
			}
            catch (System.IO.IOException ex)
            {
                throw new WebExceptionImpl(ex);
            }
		}

		public override void Flush()
		{
			try
			{
				_stream.Flush();
			}
			catch (System.Net.WebException ex)
			{
				throw new WebExceptionImpl(ex);
			}
            catch (System.IO.IOException ex)
            {
                throw new WebExceptionImpl(ex);
            }
		}

		public override int Read(byte[] buffer, int offset, int count)
		{
			try
			{
				return _stream.Read(buffer, offset, count);
			}
			catch (System.Net.WebException ex)
			{
                throw ex;
			}
            catch (System.IO.IOException ex)
            {
                throw ex;
            }
		}

		public override int ReadByte()
		{
			try
			{
				return _stream.ReadByte();
			}
			catch (System.Net.WebException ex)
			{
				throw new WebExceptionImpl(ex);
			}
            catch (System.IO.IOException ex)
            {
                throw new WebExceptionImpl(ex);
            }
		}

		public override long Seek(long offset, System.IO.SeekOrigin origin)
		{
            try
            {
                return _stream.Seek(offset, origin);
            }
            catch (System.IO.IOException ex)
            {
                throw new WebExceptionImpl(ex);
            }
		}

		public override void SetLength(long value)
		{
            try
            {
                _stream.SetLength(value);
            }
            catch (System.IO.IOException ex)
            {
                throw new WebExceptionImpl(ex);
            }
		}

		public override void Write(byte[] buffer, int offset, int count)
		{
			try
			{
				_stream.Write(buffer, offset, count);
			}
            catch (System.IO.IOException ex)
            {
                throw new WebExceptionImpl(ex);
            }
			catch (System.Net.WebException ex)
			{
				throw new WebExceptionImpl(ex);
			}
		}

		public override void WriteByte(byte value)
		{
            try
            {
                _stream.WriteByte(value);
            }
            catch (System.IO.IOException ex)
            {
                throw new WebExceptionImpl(ex);
            }
            catch (System.Net.WebException ex)
            {
                throw new WebExceptionImpl(ex);
            }
		}
	}
}
